package com.qiangesoft.captcha.config;

import com.qiangesoft.captcha.pojo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 *
 * @author qiangesoft
 * @date 2024-03-19
 */
@Slf4j
@RestControllerAdvice
public class ExceptionHandlerAdvice {

    /**
     * 运行时异常
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResultVO handleBaseException(RuntimeException exception) {
        exception.printStackTrace();
        return ResultVO.fail(exception.getMessage());
    }

    /**
     * 未知异常和错误（兜底处理）
     *
     * @param throwable
     * @return
     */
    @ExceptionHandler(Throwable.class)
    @ResponseStatus(HttpStatus.OK)
    public ResultVO handleThrowable(Throwable throwable) {
        throwable.printStackTrace();
        return ResultVO.fail("服务器内部错误");
    }
}